import * as THREE from "three";
import imgYouzi from "./imgs/youzi.png";
import runPoints from "./points";
import { OrbitControls } from "./orbitControls";
import runQiu from "./qiu";
import ParkingLot from "./parkingLot/parkingLot";
import Park from "./parkingLot/park";
import Render from "./parkingLot/render";
import IMap from "./parkingLot/IMap";
import map1 from "./parkingLot/maps/park1";
import { ModuleGraph } from "webpack";
import Pillar from "./parkingLot/pillar";

console.log(imgYouzi);
//创建场景.
let scene = new THREE.Scene();

//相机
let camera = new THREE.PerspectiveCamera(
  60,
  window.innerWidth / window.innerHeight,
  0.1,
  1000
);
camera.position.set(1500, 100, 1500);
camera.lookAt(500, 500);
//渲染器
let renderer = new THREE.WebGLRenderer();
renderer.shadowMap.enabled = true;
//设置画布大小
renderer.setSize(window.innerWidth, window.innerHeight);

//加入到body
document.body.appendChild(renderer.domElement);

// event
const ray = new THREE.Raycaster();
renderer.domElement.addEventListener("click", (e) => {
  const { clientX, clientY } = e;
  let mouse: { x: number; y: number } = { x: 0, y: 0 };
  mouse.x = (clientX / window.innerWidth) * 2 - 1;
  mouse.y = -(clientY / window.innerHeight) * 2 + 1;
  console.log(e.clientX, e.clientY, mouse);
  ray.setFromCamera(mouse, camera);

  let intersections: THREE.Intersection[] = ray.intersectObjects(
    scene.children
  );
  if (intersections.length) {
    let first = intersections[0];
    console.log(first.uv, first.object.name);
  }
});

{
  // controls

  const controls = new OrbitControls(camera, renderer.domElement);
  controls.listenToKeyEvents(window); // optional

  //controls.addEventListener( 'change', render ); // call this only in static scenes (i.e., if there is no animation loop)

  controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
  controls.dampingFactor = 0.05;

  controls.screenSpacePanning = false;

  controls.minDistance = 100;
  controls.maxDistance = 500;

  controls.maxPolarAngle = Math.PI / 2;
}

{
  const pr: Render = new Render(scene, camera);
  const paLot: ParkingLot = new ParkingLot();
  const { data, models, floors } = map1;

  // load floor
  {
    paLot.floors = floors;
  }

  // load parking space
  {
    let parks: Park[] = [];
    if (data.park && models.park) {
      parks = data.park.map((n) => {
        let { name, pos } = n;
        let pa = new Park();
        pa.pos = { ...pos };

        // get size from model
        name = name || "default";

        let mdata = models.park!.find((n) => n.name === name);
        if (mdata) {
          const { width, height } = mdata!.size;
          pa.width = width;
          pa.height = height;
        }
        return pa;
      });
    }
    paLot.parks = parks;
  }

  // load pillar
  {
    let pillars: Pillar[] = [];

    if (data.pillar && models.pillar) {
      pillars = data.pillar.map((n) => {
        let { name, pos } = n;
        let pi = new Pillar();
        pi.pos = { ...pos };

        // get size from model
        name = name || "default";

        let mdata = models.pillar!.find((n) => n.name === name);
        if (mdata) {
          const yHeight = mdata.yHeight;
          const { width, height } = mdata!.size;
          pi.width = width;
          pi.height = height;
          pi.yHeight = yHeight;
        }
        return pi;
      });
      paLot.pillars = pillars;
    }
  }

  pr.init(paLot);
}

//渲染循环
function animate() {
  requestAnimationFrame(animate);

  renderer.render(scene, camera);
  // console.log(camera.position);
}
animate();
